/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Parts;
import com.yuhanginfo.modules.sys.service.PartsService;

/**
 * 配件管理Controller
 * @author yangs
 * @version 2017-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/parts")
public class PartsController extends BaseController {

	@Autowired
	private PartsService partsService;
	
	@ModelAttribute
	public Parts get(@RequestParam(required=false) String id) {
		Parts entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = partsService.get(id);
		}
		if (entity == null){
			entity = new Parts();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:parts:view")
	@RequestMapping(value = {"list", ""})
	public String list(Parts parts, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Parts> page = partsService.findPage(new Page<Parts>(request, response), parts); 
		model.addAttribute("page", page);
		return "modules/sys/partsList";
	}

	@RequiresPermissions("sys:parts:view")
	@RequestMapping(value = "form")
	public String form(Parts parts, Model model) {
		model.addAttribute("parts", parts);
		return "modules/sys/partsForm";
	}

	@RequiresPermissions("sys:parts:edit")
	@RequestMapping(value = "save")
	public String save(Parts parts, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, parts)){
			return form(parts, model);
		}
		partsService.save(parts);
		addMessage(redirectAttributes, "保存物料管理成功");
		return "redirect:"+Global.getAdminPath()+"/sys/parts/?repage";
	}
	
	@RequiresPermissions("sys:parts:edit")
	@RequestMapping(value = "delete")
	public String delete(Parts parts, RedirectAttributes redirectAttributes) {
		partsService.delete(parts);
		addMessage(redirectAttributes, "删除物料管理成功");
		return "redirect:"+Global.getAdminPath()+"/sys/parts/?repage";
	}
   
	
	@RequiresPermissions("sys:parts:view")
	@RequestMapping(value ="partMainList")
	public String partMainList (Parts parts, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Parts> page=partsService.partMainList(new Page<Parts>(request, response), parts);
		model.addAttribute("page", page);
		return "modules/sys/carParts/partsList";
	}
	
	@RequiresPermissions("sys:parts:view")
	@RequestMapping(value ="getByCode")
	@ResponseBody
	public Parts getByCode (Parts parts, HttpServletRequest request, HttpServletResponse response, Model model) {
		return partsService.selectByCode(parts.getCode());
	}
	
	@RequiresPermissions("sys:parts:view")
	@RequestMapping(value ="getNameByCode")
	@ResponseBody
	public Parts getNameByCode(Parts parts, HttpServletRequest request, HttpServletResponse response, Model model) {
		return partsService.getNameByCode(parts.getCode());
	}
	
}